
function loadLabAssignPanel(ticketId, selectedTestTicketId) {


	$.ajax({
		type:		"GET",
		url:		"../EmerTestDiagnosisTicket/LoadLabAssignPanelAjax?set_ajax_view",
		data:		"ticketId=" + ticketId,		
		dataType:   "json",
		beforeSend: function() {
			showAjaxLoader('reloadTestAssign' + ticketId, 'innerLoadingTestAssign'  + ticketId, false);
		},
		success: 	function(data) {
			
			if (data.error.code == 'OK') {
				$('div#' + ticketId + 'InstantInfo-2').html(data.result);
				setSelectedItem("testList" + ticketId, selectedTestTicketId);
				fillSelectedLabAssignmentTicketInfo(ticketId);
	
			} else {
				displayError(data.error.mess);				
			}
			
			showAjaxLoader('reloadTestAssign' + ticketId, 'innerLoadingTestAssign'  + ticketId, true);

		}
	});	

}

function fillSelectedLabAssignmentTicketInfo(ticketId) {
	var selected = $("select#testList" + ticketId + " :selected");
	if (selected != null) {
		$("#testAssignDoctorName" + ticketId).html(selected.attr('treatment_doctor_name'));
		$("#testAssignDiagnosis" + ticketId).html(selected.attr('diagnosis'));		
	}
}

function displayLabAssignment(ticketId, idOfActionAfterCreating) {
	$.ajax({
		type:		"GET",
		url:		"../EmerTestDiagnosisTicket/BuildLabAssignmentAjax?set_ajax_view",
		data:		"ticketId=" + ticketId + "&idOfActionAfterCreating=" + idOfActionAfterCreating,		
		dataType:   "json",
		success: 	function(data) {

		if (data.error.code ==  'OK') {
			$("div#labConnectorAssignment").html(data.result);			

		} else {
			displayError(data.error.mess);				
		}

	}
	});	


}




function displayLabAssignment4VEdit(ticketId, labAssignmentTicketId, idOfActionAfterUpdating) {

	$.ajax({
		type:		"GET",
		url:		"../EmerTestDiagnosisTicket/BuildLabAssignment4VEditAjax?set_ajax_view",
		data:		"ticketId=" + ticketId 
		+ "&idOfActionAfterUpdating=" + idOfActionAfterUpdating
		+ "&labAssignmentTicketId=" + labAssignmentTicketId,		
		dataType:   "json",
		success: 	function(data) {

		if (data.error.code ==  'OK') {
			$("div#labConnectorAssignment").html(data.result);			

		} else {
			displayError(data.error.mess);				
		}

	}
	});	


}

function updateTestTicket(ticketId, testTicketId, dialog, print) {
	if (validateForSaveAssignedTest(ticketId) == false ) {
		return;
	}
	var dt=new Date();
	var second = (dt.getSeconds() < 10 ? '0' + dt.getSeconds(): dt.getSeconds());

	var requestedTime = $('#testAssignDesktop' + ticketId + '_yyyy').val() 
	+ '-' 
	+ $('#testAssignDesktop' + ticketId + '_mm').val()
	+ '-'
	+ $('#testAssignDesktop' + ticketId + '_dd').val()
	+ ' '
	+ $('#testAssignDesktopTime' + ticketId).val()
	+ ':'
	+ second;

	var paras = prepareAssignedTestForSave(ticketId, "testAssignAssignedTest" + ticketId);
	paras += '&testTicketId=' + testTicketId;
	paras += "&doctorId=" + $('#testAssignDesktopDoctorName' + ticketId + " :selected").val();
	paras += "&requestedTime=" + requestedTime;
	paras += "&diagnosis=" + $("#testAssignDesktopDiagnosis" + ticketId).val();


	$.ajax({
		type:		"GET",
		url:		"../EmerTestDiagnosisTicket/EditAssignedTestAjax?set_ajax_view",
		data:		paras,		
		dataType:   "json",
		success: 	function(data) {

		if (data.error.code ==  'OK') {
			loadLabAssignPanel(ticketId, testTicketId);
			if (print == true) {
				printAssignedTest(ticketId, testTicketId); 
			}
			dialog.dialog('close');						

		} else {
			displayError(data.error.mess);				
		}

	}
	});	

}

function displayTestsOfGroupDesktop(testId, parentId, ticketId) {

	$.ajax({
		type:		"GET",
		url:		"../EmerTestDiagnosisTicket/GetTestOfGroup?set_ajax_view",
		data:		"testGroupId=" + testId,		
		dataType:   "json",
		success: 	function(data) {			
		if (data.error.code ==  'OK') {
			var option = buildTestsDesktopSelectOption(data.result, "testAssignAssignedTest" + ticketId);
			$('select#testAssignTestOfGroup' + ticketId).html(option);

		} else {
			displayError(data.error.mess);				
		}

	}
	});

}

function moveItem(from, to) {

	$("#" + from).copyOptions("#" + to);
	$("#" + from).removeOption(/./, true);


}


function buildTestsDesktopSelectOption(arrTest, listIdToexcluse) {
	var option = "";
	var arr=[];
	$("#" + listIdToexcluse + " option").each(function() {
		arr[$(this).val()] = 'yes';
	});

	for(var testId in arrTest){
		if (arr[arrTest[testId].test_service_id] == null) {
			option += "<option value='" + arrTest[testId].test_service_id + "'>" + arrTest[testId].name + "</option>";
		}

	}
	return option;
}

function submitDeleteLabAssignmentTicket(ticketId, labAssignmentTicketId) {
	$.ajax({
		type:		"GET",
		url:		"../EmerTestDiagnosisTicket/DeleteLabAssignmentTicketAjax?set_ajax_view",
		data:		"labAssignmentTicketId=" + labAssignmentTicketId,		
		dataType:   "json",
		success: 	function(data) {

		if (data.error.code ==  'OK') {
			loadLabAssignPanel(ticketId, 0);

		} else {
			displayError(data.error.mess);				
		}

	}
	});	

}

function deleteLabAssignment(ticketId, labAssignmentTicketId) {
	promptConfirm(_("Delete"),_('Are you sure to delete this test ticket?'), function() {
			submitDeleteLabAssignmentTicket(ticketId, labAssignmentTicketId); 
			return true;
	},'YesNo','warn',_('No'));
	

}
function printAssignedTest(ticketId, assignedTestTicketId) {

	var url='../EmerTestDiagnosisTicket/PrintAssignedTestTicket?set_print_view&ticketId=' + ticketId + '&testTicketId=' + assignedTestTicketId;
	window.open(url, '', 'scrollbars=yes, menubar=no, width=800, height=600, resizable=yes, toolbar=no, location=no, status=no');

}

function loadTestItem4InputResult(ticketId, testTicketId) {
	$.ajax({
		type:		"GET",
		url:		"../EmerTestDiagnosisTicket/LoadTestItem4InputResultAjax?set_ajax_view",
		data:		"ticketId=" + ticketId + "&testTicketId=" + testTicketId,		
		dataType:   "json",
		success: 	function(data) {
		if (data.error.code ==  'OK') {
			$("div#inputTestItemResult").html(data.result);

		} else {
			displayError(data.error.mess);				
		}

	}
	});	

}

function updateDoneTestTicket(ticketId, testTicketId, isDone) {
	$.ajax({
		type:		"GET",
		url:		"../EmerTestDiagnosisTicket/UpdateDoneTestTicketAjax?set_ajax_view",
		data:		"ticketId=" + ticketId + "&testTicketId=" + testTicketId + "&isDone=" + isDone,		
		dataType:   "json",
		success: 	function(data) {

		if (data.error.code ==  'OK') {
			var selectedTestTicketId = $('#testList :selected').val();
			if (selectedTestTicketId != null && $.trim(selectedTestTicketId) != '') {
				var option = buildListTestTicketOption(data.result.testTicketList, selectedTestTicketId);
				$("select#testList").html(option);
				
			}
		} else {
			displayError(data.error.mess);				
		}

	}
	});	

}
function buildListTestTicketOption(data, selectedId) {
	var option = "";
	
	for(var index in data){
		option += "<option value='" + data[index].test_diagnosis_ticket_id +"' ";
		option += "requested_time='" + data[index].requested_time + "' ";
		option += "diagnosis='" + data[index].diagnosis + "' ";
		
		option += "doctor_name='" + data[index].doctor_name + "' ";
		option += "doctor_id='" + data[index].doctor_id + "' ";
		option += "is_done='" + data[index].is_done + "' ";
		
		if (data[index].test_diagnosis_ticket_id == selectedId) {
			option += "selected='selected' ";
		} 
		
		if (data[index].is_done == 0) {
			option += ">" + data[index].requested_time + "&nbsp;[" + _("TestTicketInProgess") +  "]</option>";			
		} else {
			option += ">" + data[index].requested_time + "&nbsp;[" + _("TestTicketDone") +  "]</option>";
		}

	}
	return option;
}

function saveInputedTestItemResult(ticketId, testTicketId) {
	var assignedTestDone = '';
	var assignedTestNotDone = '';
	$("input:checkbox[name='testItemDoneCheckbox'] ").each(function() {
		if ($(this).is(':checked') == true) {
			assignedTestDone += "&assignedTestDone[]=" + $(this).val();
		} else {
			assignedTestNotDone += "&assignedTestNotDone[]=" + $(this).val();

		}
	});



	$.ajax({
		type:		"GET",
		url:		"../EmerTestDiagnosisTicket/SaveTestItemResultAjax?set_ajax_view",
		data:		"ticketId=" + ticketId + "&testTicketId=" + testTicketId + assignedTestDone + assignedTestNotDone,		
		dataType:   "json",
		beforeSend: function() {
		$("#resultMessage").html('');			
	},
	success: 	function(data) {

		if (data.error.code ==  'OK') {
			$('#isTestTicketDone').attr('checked','checked');
			showMessage("resultMessage", _("Save successful"), 'success');
		} else {
			displayError(data.error.mess);				
		}

	}
	});	

}